package it.sd.pwframework.admin.validator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value="crudvalidator")
public class CrudValidator extends AbstractValidator implements Validator {

	public CrudValidator(){
	}
 
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

		Object customname = component.getAttributes().get("customvalidator");
		if (customname!=null) {
			Validator validator = null;
			try {
				validator = (Validator) FacesContext.getCurrentInstance().getApplication().createValidator(customname.toString());
			} catch(Exception e) {
				log.error("Error creating validator: " + customname, e);
			}
			if (validator!=null) validator.validate(context, component, value);
			
		}
 
	}
	
}
